Skip to content

Conversation

@ne0rrmatrix
Copy link
Member

Description of Change

This pull request improves the behavior of the foreground service notification in the MediaControlsService for Android by ensuring that tapping the notification brings the user back to the main app activity. The changes introduce a helper method to create the appropriate PendingIntent and attach it to the notification.

Enhancement to notification interaction:

  • The notification shown by MediaControlsService now includes a content intent, so tapping the notification will launch (or bring to front) the main app activity. This is achieved by creating a PendingIntent using the new CreateActivityPendingIntent() method and setting it with SetContentIntent on the notification builder.

Internal code improvements:

  • Added a static CreateActivityPendingIntent() method that safely constructs a PendingIntent to launch the main activity, handling potential null references and setting intent flags to ensure correct activity behavior.

Linked Issues

PR Checklist

  • Has a linked Issue, and the Issue has been approved(bug) or Championed (feature/proposal)
  • Has tests (if omitted, state reason in description)
  • Has samples (if omitted, state reason in description)
  • Rebased on top of main at time of PR
  • Changes adhere to coding standard
  • Documentation created or updated: https://github.com/MicrosoftDocs/CommunityToolkit/pulls

Additional information

Fixes a long standing issue where clicking on notification after leaving app does not restore app. This fixes that issue and restores expected default android behavior.

Copilot AI review requested due to automatic review settings December 1, 2025 09:03
@ne0rrmatrix ne0rrmatrix requested review from TheCodeTraveler and removed request for Copilot December 1, 2025 09:04
justcoding121 added a commit to justcoding121/bible-alarm that referenced this pull request Dec 1, 2025
Copilot AI review requested due to automatic review settings December 6, 2025 19:15
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request fixes notification click behavior in the MediaControlsService for Android by adding a content intent that properly restores the main app activity when the user taps the foreground service notification.

Key changes:

  • Added CreateActivityPendingIntent() helper method to construct the appropriate PendingIntent
  • Integrated the PendingIntent into the notification builder via SetContentIntent()

@ne0rrmatrix ne0rrmatrix added the needs discussion Discuss it on the next Monthly standup label Jan 8, 2026
Copilot AI review requested due to automatic review settings January 8, 2026 20:02
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

@TheCodeTraveler TheCodeTraveler removed the needs discussion Discuss it on the next Monthly standup label Jan 14, 2026
@ne0rrmatrix ne0rrmatrix removed the request for review from TheCodeTraveler January 18, 2026 14:44
Copilot AI review requested due to automatic review settings January 19, 2026 15:55
@ne0rrmatrix ne0rrmatrix enabled auto-merge (squash) January 19, 2026 15:55
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.

Comment on lines +184 to +186
var packageName = Platform.AppContext.PackageName ?? throw new InvalidOperationException("PackageName cannot be null");
var packageManager = Platform.AppContext.PackageManager ?? throw new InvalidOperationException("PackageManager cannot be null");
var launchIntent = packageManager.GetLaunchIntentForPackage(packageName) ?? throw new InvalidOperationException("Launch intent cannot be null");
Copy link

Copilot AI Jan 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the project's coding guidelines, null checking should use the is pattern instead of the null-coalescing operator with throw. Consider refactoring these null checks to use pattern matching for consistency with the codebase standards.

For example, instead of:

var packageName = Platform.AppContext.PackageName ?? throw new InvalidOperationException("PackageName cannot be null");

Use:

if (Platform.AppContext.PackageName is not { } packageName)
{
    throw new InvalidOperationException("PackageName cannot be null");
}

This pattern should be applied to all three null checks in this method (PackageName, PackageManager, and the launch intent).

Copilot generated this review using guidance from repository custom instructions.
Comment on lines +191 to +192
return PendingIntent.GetActivity(Platform.AppContext, 0, launchIntent, flags)
?? throw new InvalidOperationException("PendingIntent cannot be null");
Copy link

Copilot AI Jan 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the project's coding guidelines, null checking should use the is pattern instead of the null-coalescing operator with throw. Consider refactoring this null check to use pattern matching for consistency with the codebase standards.

For example, instead of:

return PendingIntent.GetActivity(Platform.AppContext, 0, launchIntent, flags) ?? throw new InvalidOperationException("PendingIntent cannot be null");

Use:

if (PendingIntent.GetActivity(Platform.AppContext, 0, launchIntent, flags) is not { } pendingIntent)
{
    throw new InvalidOperationException("PendingIntent cannot be null");
}
return pendingIntent;

Copilot generated this review using guidance from repository custom instructions.

launchIntent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);

var flags = PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable;
Copy link

Copilot AI Jan 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PendingIntentFlags.Immutable flag was introduced in Android 12 (API 31), but this service supports Android 26+ (as indicated by the [SupportedOSPlatform("Android26.0")] attribute). Using this flag unconditionally will cause runtime errors on Android versions 26-30.

Consider adding a version check to use the appropriate flags:

var flags = OperatingSystem.IsAndroidVersionAtLeast(31)
    ? PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable
    : PendingIntentFlags.UpdateCurrent;

Alternatively, if Android 12+ is now the minimum supported version, update the SupportedOSPlatform attribute on the class accordingly.

Suggested change
var flags = PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable;
var flags = OperatingSystem.IsAndroidVersionAtLeast(31)
? PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable
: PendingIntentFlags.UpdateCurrent;

Copilot uses AI. Check for mistakes.
@ne0rrmatrix ne0rrmatrix merged commit 987b89d into CommunityToolkit:main Jan 19, 2026
16 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jan 21, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Android 14+ Notification Body Tap Does Nothing

3 participants